#/usr/bin/tclsh
proc sleep {N} {
   after [expr {int($N * 100)}]
}




source [file dirname [info script]]/define.tcl
set clear_reset		0






set code_ram_size_in_byte [expr 1 <<($COD_RAM_ADDR_WIDTH+2)]
set code_ram_size_in_word [expr 1 <<($COD_RAM_ADDR_WIDTH)]

scan $NIOS2_RESET_ADDR "%x"  NIOS2_RESET_HEX
#puts $NIOS2_RESET_HEX 
#puts $code_ram_size_in_byte



set ram_required	[expr 1 << ($NIOS_RAM_WIDTH-$COD_RAM_ADDR_WIDTH)]
#puts $ram_required

#puts "read args"
# read core address and programe file 
set usage "prog.tcl ?-x_axe number? -y_axe number ?   -filename filename ? -l"
set i [llength $argv]
set j	0
if { $i == 0} { error $usage}
while {$i>0} {
    if {[lindex $argv $j] eq "-x_axe"} {
        set x_axe [lindex $argv [expr {$j + 1}]]
       # puts $x_axe
    	 set i [expr {$i - 2}]
	 set j [expr {$j + 2}]
     
	} elseif {[lindex $argv $j] eq "-y_axe"} {
        set y_axe [lindex $argv [expr {$j + 1}]]
      #	puts $y_axe
    	 set i [expr {$i - 2}]
	 set j [expr {$j + 2}]
     
	} elseif {[lindex $argv $j] eq "-filename"} { 
	 set file_name [lindex $argv [expr {$j + 1}]]
       #  puts $file_name
    	 set i [expr {$i - 2}]
	 set j [expr {$j + 2}]
	
	} elseif {[lindex $argv $j] eq "-l"} { 
		set clear_reset 	1
		set i [expr {$i - 1}]
	

	} else { error $usage
	}

 
  } 


set CORE_ADDR [expr ((($x_axe << $Y_NODE_NUM_WIDTH) + $y_axe) )]




# List all available programming hardware, and select the USB-Blaster.
# (Note: this example assumes only one USB-Blaster is connected.)
#puts "Programming Hardware:"
foreach hardware_name [get_hardware_names] {
	#puts $hardware_name
	if { [string match "USB-Blaster*" $hardware_name] } {
		set usbblaster_name $hardware_name
	}
}
#puts "\nSelect JTAG chain connected to $usbblaster_name.\n";

# List all devices on the chain, and select the first device on the chain.
#puts "\nDevices on the JTAG chain:"
foreach device_name [get_device_names -hardware_name $usbblaster_name] {
	#puts $device_name
	if { [string match "@1*" $device_name] } {
		set test_device $device_name
	}
}

# Initiate a editing sequence
begin_memory_edit -hardware_name $usbblaster_name -device_name $test_device


update_cmd_mem  $x_axe $y_axe 0 $code_ram_size_in_word 0 


#resetting all niose
 send_cmd $RESET_CODE 


set i 0

for {set i 0} {$i<$ram_required} {incr i} {
	#change elf to hex 	
	set start_addr		 [expr $NIOS2_RESET_HEX+ ($i* $code_ram_size_in_byte)]
	set end_addr		 [expr $NIOS2_RESET_HEX+ (($i+1) * $code_ram_size_in_byte)-1];	
#	../software/blink_led/blink_led.elf
	set cmd 			 [  format " elf2hex $file_name 0x%X 0x%X  --width=32 --little-endian-mem --create-lanes=0  mem_init/ram%d.hex"\
 $start_addr  $end_addr  $i]	
	eval exec  $cmd
	#update prog memory
	 update_content_to_memory_from_file -instance_index 0 -mem_file_path mem_init/ram$i.hex -mem_file_type hex
	#reset all microes
	

# send ram data to nios memory
set cmd_error 	0
if { $i == 0} { 
	set cmd_error [send_cmd $FIRST_PROG_CODE]
} elseif { $i == [ expr $ram_required-1]} { 
	set cmd_error [send_cmd $TAIL_PROG_CODE]
} else { 
	set cmd_error [send_cmd $BODY_PROG_CODE]
}
	
	if { $cmd_error == 1} {
		set i [expr $ram_required+5]
		puts "------------------------------fail to programe !----------------------------------" 
		end_memory_edit
		exit -1

	} 



	#set prog_done	0
	#set count	0
	#while { $prog_done ==  0 && $count <20 } {
		#set read_status  [ read_content_from_memory -instance_index 1 -start_address 0 -word_count 1 ]
                #set prog_done [string index $read_status 1]
                #incr count
		#puts $prog_done 
		#puts $read_status
	#}


	#if { $count > 19} {
		#set i [expr $ram_required+5]
		#puts "------------------------------fail to programe !----------------------------------" 
		#end_memory_edit
		#exit -1
		
	#} 

	
}



if { $clear_reset == 1 } { send_cmd $START_CODE }


#End the editing sequence
end_memory_edit



	
puts "------------------------------program has been done successfully !----------------------------------"

exit 0








